<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button>清除定时器</button>
    <script>
      var btn = document.querySelector('button');
      var timr = setInterval(function () {
        alert('我给你一巴掌');
      }, 1500);
      //清除定时器
      document.querySelector('button').addEventListener('click', function () {
        clearInterval(timr);
      });

      //   clearInterval(timr);
    </script>
  </body>
</html>
